package cn.boyu.service.impl;

import cn.boyu.entity.*;
import cn.boyu.exception.LogicException;
import cn.boyu.mapper.CustomerMapper;
import cn.boyu.query.CustomerQuery;
import cn.boyu.result.IDUtils;
import cn.boyu.service.IAssetsAmmeterService;
import cn.boyu.service.ICustomerService;
import cn.boyu.service.ISysDictDataService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author yn
 * @since 2022-11-22
 */
@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements ICustomerService {


    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private IAssetsAmmeterService ammeterService;
    @Autowired
    private ISysDictDataService dataService;

    @Override
    public IPage<Customer> queryPage(CustomerQuery qo) {
        IPage<Customer> page = new Page<>(qo.getCurrentPage(), qo.getPageSize());
        QueryWrapper<Customer> qw = new QueryWrapper<>();
        qw.eq(qo.getCustId() != null, "cust_id", qo.getCustId());
        qw.eq("del_flg", 0);
        IPage<Customer> result = page(page, qw);
        for (Customer customer : result.getRecords()) {
            List<SysDictData> electrovalence = dataService.getType("electrovalence", customer.getElectrovalence());
            List<SysDictData> manageCompany = dataService.getType("manageCompany", customer.getManageCompany());
            List<SysDictData> custType = dataService.getType("custType", customer.getCustType());
            if (custType.size() != 0 && manageCompany.size() != 0 && electrovalence.size() != 0) {
                customer.setElectrovalenceString(electrovalence.get(0).getDictLabel());
                customer.setCustTypeString(custType.get(0).getDictLabel());
                customer.setManageCompanyString(manageCompany.get(0).getDictLabel());
            }
        }
        return result;
    }

    @Override
    public void batchDelete(Long[] ids) {

        customerMapper.batchDelete(ids);
    }

    @Override
    public void update(Customer customer) {
        customerMapper.updateById(customer);
    }

    @Override
    public void savecust(Customer customer) {
        //默认生成用户编号
        customer.setCustId(IDUtils.createID());
        //生成创建日期
        customer.setCreateTime(new Date());
        customerMapper.insert(customer);
    }

    @Override
    public void delete(Long customerId) {
        System.out.println("customer:" + customerId);

        ammeterService.update(
                new UpdateWrapper<AssetsAmmeter>()
                        .set("customer_id", null)
                        .eq("customer_id",customerId));
        update(new UpdateWrapper<Customer>().set("del_flg", 1).eq("id", customerId));

    }


}
